#!/usr/bin/env python
# encoding=utf-8

from flask import *

from . import api
from .. import dao


@api.route('/api/search/<key>/page/<int:page>')
def search(key, page):
    total, data = dao.select_es(key, page)
    result = {
        'total': total,
        'key': key,
        'currentPage': page,
        'data': data
    }
    return jsonify(result)


@api.route('/api/detail/<sid>')
def detail(sid):
    data = dao.render_anno_db(dao.select_db_single('stockAnnouncement', {'_id': sid}))
    file_meta = dao.select_db_single('fileMetaData', {'_id': data['file']}, db='qiniu')
    if not file_meta:
        return Response('Server error.', 500)
    data['filesize'] = file_meta.get('filesize', 0)
    return jsonify(data)


@api.route('/api/health_check')
def health_check():
    return ''
